﻿using Excel;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using UnityEngine;

public class ExcelTool : MonoBehaviour
{
    private static ExcelTool instance;
    public static ExcelTool Instance { get => instance;}

    //public Dictionary<string, LevelData> levels = new Dictionary<string, LevelData>();
    //public Dictionary<string, EnemyData> enemys = new Dictionary<string, EnemyData>();
    //public Dictionary<string, BuffData> buffs = new Dictionary<string, BuffData>();
    //public List<ShooterData> shooters = new List<ShooterData>();

    public Dictionary<string, Eyes> eyesData = new Dictionary<string, Eyes>();
    public Dictionary<int, Animal> animalData = new Dictionary<int, Animal>();

    private void Awake()
    {
        if(instance==null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
            //DataLoad();
            MyLoadData();
        }
        else
        {
            Debug.Log("表为空！");
        }
    }
    public void MyLoadData()
    {
        DataRowCollection eyes=ReadExcel(Application.dataPath + "/Excel/config.xlsx", "eyes");
        for (int i = 1; i < eyes.Count; i++)
        {
            if (eyes[i][0].ToString()=="")
            {
                //判断数据是否为空
                break;
            }
            Eyes e = new Eyes();
            e.ID = float.Parse(eyes[i][0].ToString());
            e.realName = eyes[i][1].ToString();
            e.unlock_Gold= float.Parse(eyes[i][2].ToString());
            e.unlock_Stage= float.Parse(eyes[i][3].ToString());
            e.up_Gold= float.Parse(eyes[i][4].ToString());
            e.up_glod_Growth= float.Parse(eyes[i][5].ToString());
            e.basic_add_Score= float.Parse(eyes[i][6].ToString());
            e.up_add_Score= float.Parse(eyes[i][7].ToString());
            eyesData.Add(e.realName,e);
            
        }

        DataRowCollection animal = ReadExcel(Application.dataPath + "/Excel/config.xlsx", "animal");
        for (int i = 1; i < animal.Count; i++)
        {
            if (animal[i][0].ToString()=="")
            {
                break;
            }
            Animal a = new Animal();
            a.ID = int.Parse(animal[i][0].ToString());
            a.realName = animal[i][1].ToString();
            a.hp = int.Parse(animal[i][2].ToString());
            a.dropGold = int.Parse(animal[i][3].ToString());
            animalData.Add(a.ID,a);
           // Debug.Log("a.id:"+a.ID);
        }

    }


    #region 原本加载的方式
    //public void DataLoad()
    //{
    //    DataRowCollection levelData = ReadExcel(Application.dataPath + "/Excel/config.xlsx", "regular");
    //    for (int i = 1; i < levelData.Count; i++)
    //    {
    //        if(levelData[i][0].ToString()=="")
    //        {
    //            break;
    //        }
    //        LevelData level = new LevelData();
    //        level.interval = levelData[i][1].ToString();
    //        level.max = float.Parse(levelData[i][2].ToString());
    //        level.suol_max = float.Parse(levelData[i][3].ToString());
    //        level.soldier_id = levelData[i][4].ToString();
    //        level.soldier_lv = levelData[i][5].ToString();
    //        level.soldier_1 = levelData[i][6].ToString();
    //        level.soldier_2 = levelData[i][7].ToString();
    //        level.soldier_3 = levelData[i][8].ToString();
    //        level.soldier_4 = levelData[i][9].ToString();
    //        level.soldier_5 = levelData[i][10].ToString();
    //        level.soldier_6 = levelData[i][11].ToString();
    //        level.soldier_7 = levelData[i][12].ToString();
    //        level.boos = levelData[i][13].ToString();
    //        level.mapId = levelData[i][14].ToString();
    //        level.minssion_diamond = levelData[i][15].ToString();
    //        levels.Add(levelData[i][0].ToString(), level);
    //    }
    //    DataRowCollection shooterData = ReadExcel(Application.dataPath + "/Excel/config.xlsx", "shooter");
    //    for (int i = 1; i < shooterData.Count; i++)
    //    {
    //        if (levelData[i][0].ToString() == "")
    //        {
    //            break;
    //        }
    //        ShooterData shooter = new ShooterData();
    //        shooter.id= shooterData[i][0].ToString();
    //        shooter.type = float.Parse(shooterData[i][1].ToString());
    //        shooter.realname = shooterData[i][2].ToString();
    //        shooter.atk_distance = float.Parse(shooterData[i][3].ToString());
    //        shooter.atk_type = float.Parse(shooterData[i][4].ToString());
    //        shooter.atk_growth_type = float.Parse(shooterData[i][5].ToString());
    //        shooter.atk_attach = float.Parse(shooterData[i][6].ToString());
    //        shooter.atk_growth_attach = float.Parse(shooterData[i][7].ToString());
    //        shooter.att_interval = float.Parse(shooterData[i][8].ToString());
    //        shooter.unlock_diamond = float.Parse(shooterData[i][9].ToString());
    //        shooter.up_diamond = float.Parse(shooterData[i][10].ToString());
    //        shooter.unlock_level = float.Parse(shooterData[i][11].ToString());
    //        shooter.up_diamond_growth = float.Parse(shooterData[i][12].ToString());
    //        shooter.drop_shooter = float.Parse(shooterData[i][13].ToString());
    //        shooter.bullet_type = float.Parse(shooterData[i][14].ToString());
    //        shooter.num = float.Parse(shooterData[i][15].ToString());
    //        shooter.param = shooterData[i][16].ToString();
    //        shooter.speed = float.Parse(shooterData[i][17].ToString());
    //        shooter.range = shooterData[i][18].ToString();
    //        shooter.dam_max = float.Parse(shooterData[i][19].ToString());
    //        shooter.buff = shooterData[i][20].ToString();
    //        shooters.Add(shooter);
    //    }
    //    DataRowCollection enemyData = ReadExcel(Application.dataPath + "/Excel/config.xlsx", "enemy");
    //    for (int i = 1; i < enemyData.Count; i++)
    //    {
    //        if (enemyData[i][0].ToString() == "")
    //        {
    //            break;
    //        }
    //        EnemyData enemy = new EnemyData();
    //        //enemy.id = data[i][0].ToString();
    //        enemy.def_spe = float.Parse(enemyData[i][1].ToString());
    //        enemy.def_spe_type = float.Parse(enemyData[i][2].ToString());
    //        enemy.speed = float.Parse(enemyData[i][3].ToString());
    //        enemy.realName = enemyData[i][4].ToString();
    //        enemy.def = float.Parse(enemyData[i][5].ToString());
    //        enemy.hp = float.Parse(enemyData[i][6].ToString());
    //        enemy.def_spe_growth = float.Parse(enemyData[i][7].ToString());
    //        enemy.def_growth = float.Parse(enemyData[i][8].ToString());
    //        enemy.hp_growth = float.Parse(enemyData[i][9].ToString());
    //        enemy.soul = float.Parse(enemyData[i][10].ToString());
    //        enemy.dropG = float.Parse(enemyData[i][11].ToString());
    //        enemys.Add(enemyData[i][0].ToString(), enemy);
    //    }
    //    DataRowCollection buffdata= ReadExcel(Application.dataPath + "/Excel/config.xlsx", "buff");
    //    for (int i = 1; i < buffdata.Count; i++)
    //    {
    //        if (buffdata[i][0].ToString() == "")
    //        {
    //            break;
    //        }
    //        BuffData buff = new BuffData();
    //        buff.type= float.Parse(buffdata[i][1].ToString());
    //        buff.des= buffdata[i][2].ToString();
    //        buff.time= float.Parse(buffdata[i][3].ToString());
    //        buff.probability= float.Parse(buffdata[i][4].ToString());
    //        buffs.Add(buffdata[i][0].ToString(),buff);
    //    }
    //}

    #endregion

    #region -- 自定义函数
    /// <summary>
    /// 读取 Excel 表并返回一个 DataRowCollection 对象
    /// </summary>
    /// <param name="_path">Excel 表路径</param>
    /// <param name="_sheetIndex">读取的 Sheet 名称。Excel 表中是有多个 Sheet 的</param>
    /// <returns></returns>
    private static DataRowCollection ReadExcel(string _path, string _sheetName)
    {
        FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read, FileShare.Read);
        //IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);//读取 Excel 1997-2003版本
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);//读取 2007及以后的版本
        DataSet result = excelReader.AsDataSet();
        return result.Tables[_sheetName].Rows;
    }
    #endregion
}

#region 原本加载方式
//public class LevelData
//{
//    public string interval;//出兵间隔
//    public float max;//最大出兵数量
//    public float suol_max;//小兵的生命数量上限
//    public string soldier_id;//小兵类型
//    public string soldier_lv;//小兵等级
//    public string soldier_1;//
//    public string soldier_2;
//    public string soldier_3;
//    public string soldier_4;
//    public string soldier_5;
//    public string soldier_6;
//    public string soldier_7;
//    public string boos;//Boss的id和等级
//    public string mapId;
//    public string minssion_diamond;
//}
//public class ShooterData
//{
//    public string id;//
//    public float type;//
//    public string realname;//名字
//    public float atk_distance;//攻击距离触发区间
//    public float atk_type;//当前类型的伤害数值
//    public float atk_growth_type;//
//    public float atk_attach;//伤害数值
//    public float atk_growth_attach;
//    public float att_interval;//攻击频率
//    public float unlock_diamond;//解锁需要的钻石
//    public float unlock_level;//解锁需要的关卡
//    public float up_diamond;//升星所需要的钻石
//    public float up_diamond_growth;//升星的升级系数
//    public float drop_shooter;//塔的生成几率
//    public float bullet_type;//伤害类型
//    public float num;//单次攻击的子弹数量
//    public string param;//特殊状态攻击
//    public float speed;//速度
//    public string range;//伤害范围
//    public float dam_max;//单次攻击的伤人数量
//    public string buff;//buffID
//}
//public class EnemyData
//{
//    //public string id;//id
//    public string realName;//名字
//    public float hp;//基础生命
//    public float hp_growth;//升级生命系数
//    public float def;//物理防御
//    public float def_growth;//升级防御系数
//    public float def_spe;//特殊防御
//    public float def_spe_type;//特殊防御类型
//    public float def_spe_growth;//升级特殊防御系数
//    public float speed;//速度
//    public float soul;//灵魂
//    public float dropG;//掉落金币基础
//}
//public class BuffData
//{
//    public string id;
//    public float type;
//    public string des;
//    public float time;
//    public float probability;
//}
#endregion

public struct Eyes
{
    public float ID;//id
    public string realName;//读取的名字
    public float unlock_Gold;//金币资源
    public float unlock_Stage;//第几关卡
    //升级金币=基础金币+2的（升级等级-1）平方*金币系数
    public float up_Gold;//基础金币  
    public float up_glod_Growth;//金币系数
    //升级后的附加属性=基础附加属性+（升级等级-1）*升级附加属性系数
    public float basic_add_Score;//基础附加属性
    public float up_add_Score;//升级附加属性系数
}

public class Animal
{
    public int ID;//id
    public string realName;//读取的名字
    public int hp;//血量
    public int dropGold;//过关的金币
}
